#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
i64 n, m;
i64 ans;



void solve()
{
    cin >> n >> m;
    if (m == 1) {
        if (n % 2 == 1) {
            cout << 1 << "\n";
        }
        else {
            cout << 2 << "\n";
        }
        return;
    }
    ans = n / 2 + 1;
    i64 cnt = m - ans;
    if (m < ans) cout << n << "\n";
    else         cout << n - cnt * 2 << "\n";
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--) {
        solve();
    }
}